package com.dwin.h5.app.h5;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.Map;

public class X5WebView extends WebView {
    private static final String TAG = "X5WebView";

    //    上下文
    private Context mContext;
    //    回调接口
    private OnWebViewListener onWebViewListener;

    public void setOnWebViewListener(OnWebViewListener onWebViewListener) {
        this.onWebViewListener = onWebViewListener;
    }

    public X5WebView(Context context) {
        super(context);
        init(context);
    }

    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i, boolean b) {
        super(context, attributeSet, i, b);
        init(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i, Map<String, Object> map, boolean b) {
        super(context, attributeSet, i, map, b);
        init(context);
    }

    public MyJaveScriptInterface javeScriptInterface;

    private void init(Context context) {
        this.mContext = context;
//      打开此代码可使移动设备链接 chrome 调试
//        setWebContentsDebuggingEnabled(true);
        initWebViewSettings(); // webView 设置
        javeScriptInterface = new MyJaveScriptInterface(mContext); //设置 jsBridge
        addJavascriptInterface(javeScriptInterface, "androidJSBridge");
        initWebViewClient(); // webClient 设置
        initChromeClient(); // chromeClient 设置
    }

    /**
     * webView 设置
     */
    private void initWebViewSettings() {
        WebSettings webSettings = getSettings();
//        允许运行 js 代码
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        String appCachePath = ((Activity) mContext).getApplication().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAppCacheEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

//        不可缩放
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setDisplayZoomControls(true);
//        设置缓存策略
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    }

    /**
     * webClient 设置
     */
    private void initWebViewClient() {
//        设置网页在APP 内部打开，而不是用外部浏览器
        setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                Log.d(TAG, "onPageFinished");
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
                Log.d(TAG, "onPageStarted");
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
                // 页面加载失败
//                Log.d(TAG, "onPageStarted 09090");
            }
        });
    }

    /**
     * chromeClient 设置
     */
    private void initChromeClient() {
        setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
//                回调网页加载状态
                if (onWebViewListener != null) {
                    onWebViewListener.onProgressChanged(webView, i);
                }
            }

            @Override
            public void onReceivedTitle(WebView webView, String title) {
                super.onReceivedTitle(webView, title);
                if (onWebViewListener != null) {
                    onWebViewListener.onReceiveTitle(title);
                }
            }

            /**
             * 监听alert弹出框，使用原生弹框代替alert。
             */
            @Override
            public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage(s1);
                builder.setNegativeButton("确定", null);
                builder.create().show();
                jsResult.confirm();
                return true;
            }
        });
    }

    public interface OnWebViewListener {
        void onProgressChanged(WebView webView, int progress);

        void onReceiveTitle(String title);
    }
}
